<?php

include_once(dirname(__FILE__) . '/PGDB.php');
include_once(dirname(__FILE__) . '/Tools.php');

/**
 * 
 * 省市管理工具类
 * 
 * 
 * 
 * 完成数据库的操作。 
 * @author      m17269@163.com
 * @version     1.0
 * @since       1.0 
 */

class Management
{

    protected $_pgdb = null;

    /**
     *  基本的构造方法，加载数据库配置，如果没填写则使用默认的
     */
    public function __construct()
    {
        $this->_pgdb = PGDB::getInstance();
    }

    /**
     * 获取省的信息
     */
    public function getProvinces(array $data = array('limit' => 0, 'offset' => 0)): string
    {
        if (empty($data['limit'])) {
            $sql = "select * from public.management_t where par_id = 0 order by id";
        } else {
            $sql = "select * from public.management_t where par_id = 0 order by id limit $data[limit] offset ";
            $sql .= (isset($data['offset']) ? $data['offset'] : 0);
        }
        $res = $this->_pgdb->query($sql);
        $provinces = array();
        while ($row = pg_fetch_row($res)) {
            $provinces[] = $row;
        }
        if (empty($provinces)) {
            Tools::setHttpHeaders(403);
            return Tools::returnToJson(0x000001, '获取省的信息失败，或者没有任何的数据', array());
        }
        return Tools::returnToJson(0x000000, '获取省的信息成功', $provinces);
    }

    /**
     * 获取该省所有市的信息
     */
    public function getCity(array $data = array('id' => 0, 'limit' => 0, 'offset' => 0)): string
    {
        if (empty($data['id'])) {
            Tools::setHttpHeaders(403);
            return Tools::returnToJson(0x000001, '获取市的信息失败，或者没有任何的数据', array());
        }
        if (empty($data['limit'])) {
            $sql = "select * from public.management_t where par_id = " . $data['id'] . " order by id";
        } else {
            $sql = "select * from public.management_t where par_id = " . $data['id'];
            $sql .= " order by id limit " . $data['limit'] . " offset " . (isset($data['offset']) ? $data['offset'] : 0);
        }
        $res = $this->_pgdb->query($sql);
        $citys = array();
        while ($row = pg_fetch_row($res)) {
            $citys[] = $row;
        }
        if (empty($citys)) {
            Tools::setHttpHeaders(403);
            return Tools::returnToJson(0x000001, '获取市的信息失败，或者没有任何的数据', array());
        }
        return Tools::returnToJson(0x000000, '获取市的信息成功', $citys);
    }

    /**
     * 获取地区
     */
    public function getArea(array $data = array('id' => 0, 'limit' => 0, 'offset' => 0)): string
    {
        if (empty($data['id'])) {
            Tools::setHttpHeaders(403);
            return Tools::returnToJson(0x000001, '获取市的信息失败，或者没有任何的数据', array());
        }
        if (empty($data['limit'])) {
            $sql = "select * from public.management_t where par_id = " . $data['id'] . " order by id";
        } else {
            $sql = "select * from public.management_t where par_id = " . $data['id'];
            $sql .= " order by id limit " . $data['limit'] . " offset " . (isset($data['offset']) ? $data['offset'] : 0);
        }
        $res = $this->_pgdb->query($sql);
        $areas = array();
        while ($row = pg_fetch_row($res)) {
            $areas[] = $row;
        }
        if (empty($areas)) {
            Tools::setHttpHeaders(403);
            return Tools::returnToJson(0x000001, '获取市的信息失败，或者没有任何的数据', array());
        }
        return Tools::returnToJson(0x000000, '获取市的信息成功', $areas);
    }


    /**
     * 增加省
     */
    public function addProvince(array $data = array('province' => '')): string
    {
        $sql = "insert into public.management_t(name,par_id) VALUES('" . $data['province'] . "',0);";
        if (empty($data['province'])) {
            Tools::setHttpHeaders(403);
            return Tools::returnToJson(0x000001, '添加失败，或并没有上传数据');
        }
        $res = @$this->_pgdb->query($sql);
        if (!$res) {
            Tools::setHttpHeaders(403);
            die(Tools::returnToJson(0x000002, '添加省失败'));
        }
        return Tools::returnToJson(0x000000, '添加省成功');
    }

    /**
     * 删除省
     */
    public function delProvince(array $data = array('id' => 0)): string
    {
        // 1.查看数据是否齐全
        if (empty($data['id'])) {
            Tools::setHttpHeaders(403);
            return Tools::returnToJson(0x000001, '删除失败，或并没有上传数据');
        }
        // 2.获取到省下所有的市ID
        $get_city_sql = "select * from public.management_t where par_id = " . $data['id'];
        $city_res = @$this->_pgdb->query($get_city_sql);
        while ($city_row = (pg_fetch_row($city_res))) {
            // 3. 根据市的ID获取所有的区ID
            $area_sql = "select * from public.management_t where par_id = $city_row[0]";
            $area_res = @$this->_pgdb->query($area_sql);
            // 4. 开始删除区
            while ($area_row = (pg_fetch_row($area_res))) {
                $area_del_sql = "delete from public.management_t where id = $area_row[0]";
                $res = @$this->_pgdb->query($area_del_sql);
                if (!$res) {
                    Tools::setHttpHeaders(403);
                    return Tools::returnToJson(0x000002, '删除区失败');
                }
            }
            // 5. 删除市
            $city_del_sql = "delete from public.management_t where id = $city_row[0]";
            $res = @$this->_pgdb->query($city_del_sql);
            if (!$res) {
                Tools::setHttpHeaders(403);
                return Tools::returnToJson(0x000002, '删除市失败');
            }
        }
        // 6. 删除省
        $del_provinces_sql = "delete from public.management_t where id = " . $data['id'];
        $res = @$this->_pgdb->query($del_provinces_sql);
        if (!$res) {
            Tools::setHttpHeaders(403);
            return Tools::returnToJson(0x000002, '删除省失败');
        }
        return Tools::returnToJson(0x000000, '删除省完成');
    }

    /**
     * 编辑省
     */
    public function editProvince(array $data = array('id' => 0, 'province' => '')): string
    {
        $edit_provinces_sql = "update public.management_t set name = '";
        $edit_provinces_sql .= $data['province'] . "' where id = " . $data['id'];
        $res = @$this->_pgdb->query($edit_provinces_sql);
        // 4、返回结果
        if (!$res) {
            Tools::setHttpHeaders(403);
            return Tools::returnToJson(0x000002, '编辑省失败');
        }
        return Tools::returnToJson(0x000000, '编辑省完成');
    }


    /**
     * 增加市
     */
    public function addCity(array $data = array('city' => '', 'par_id' => 0)): string
    {
        if (empty($data['city']) || empty($data['par_id'])) {
            Tools::setHttpHeaders(403);
            return Tools::returnToJson(0x000002, '添加市失败，或并没有上传数据');
        }

        $sql = "insert into public.management_t(name,par_id) VALUES('";
        $sql .= $data['city'] . "'," . $data['par_id'] . ");";
        $res = @$this->_pgdb->query($sql);
        if (!$res) {
            Tools::setHttpHeaders(403);
            die(Tools::returnToJson(0x000002, '添加市失败，重复添加'));
        }
        return Tools::returnToJson(0x000000, '添加市成功');
    }

    /**
     * 删除市
     */
    public function delCity(array $data = array('id' => '')): string
    {
        if (empty($data['id'])) {
            Tools::setHttpHeaders(403);
            return Tools::returnToJson(0x000002, '刪除市失败，或并没有上传数据');
        }
        // 2.获取到市下所有的区ID
        $get_area_sql = "select * from public.management_t where par_id = " . $data['id'];
        $area_res = @$this->_pgdb->query($get_area_sql);
        while ($area_row = (pg_fetch_row($area_res))) {
            // 3. 开始删除区
            $area_del_sql = "delete from public.management_t where id = $area_row[0]";
            $res = @$this->_pgdb->query($area_del_sql);
            if (!$res) {
                Tools::setHttpHeaders(403);
                die(Tools::returnToJson(0x000002, '删除区失败'));
            }
        }
        // 4. 删除市
        $del_city_sql = "delete from public.management_t where id = " . $data['id'];
        $res = @$this->_pgdb->query($del_city_sql);
        if (!$res) {
            Tools::setHttpHeaders(403);
            die(Tools::returnToJson(0x000002, '删除市失败'));
        }
        die(Tools::returnToJson(0x000000, '删除市完成'));
    }


    /**
     * 编辑市
     */
    public function editCity(array $data = array('id' => 0, 'city' => '')): string
    {
        $edit_provinces_sql = "update public.management_t set name = '";
        $edit_provinces_sql .= $data['city'] . "' where id = " . $data['id'];
        $res = @$this->_pgdb->query($edit_provinces_sql);
        // 返回结果
        if (!$res) {
            Tools::setHttpHeaders(403);
            return Tools::returnToJson(0x000002, '编辑市失败');
        }
        return Tools::returnToJson(0x000000, '编辑市完成');
    }

    /**
     * 增加区
     */
    public function addArea(array $data = array('area' => '', 'par_id' => 0)): string
    {
        if (empty($data['area']) || empty($data['par_id'])) {
            Tools::setHttpHeaders(403);
            return Tools::returnToJson(0x000002, '添加区失败，或并没有上传数据');
        }

        $sql = "insert into public.management_t(name,par_id) VALUES('";
        $sql .= $data['area'] . "'," . $data['par_id'] . ");";
        $res = @$this->_pgdb->query($sql);
        if (!$res) {
            Tools::setHttpHeaders(403);
            die(Tools::returnToJson(0x000002, '添加区失败，重复添加'));
        }
        return Tools::returnToJson(0x000000, '添加区成功');
    }


    /**
     * 删除区
     */
    public function delArea(array $data = array('id' => '')): string
    {
        if (empty($data['id'])) {
            Tools::setHttpHeaders(403);
            return Tools::returnToJson(0x000002, '刪除区失败，或并没有上传数据');
        }
        // 4. 删除市
        $del_area_sql = "delete from public.management_t where id = " . $data['id'];
        $res = @$this->_pgdb->query($del_area_sql);
        if (!$res) {
            Tools::setHttpHeaders(403);
            die(Tools::returnToJson(0x000002, '删除区失败'));
        }
        die(Tools::returnToJson(0x000000, '删除区完成'));
    }

    /**
     * 编辑区
     */
    public function editArea(array $data = array('id' => 0, 'area' => '')): string
    {
        $edit_provinces_sql = "update public.management_t set name = '";
        $edit_provinces_sql .= $data['area'] . "' where id = " . $data['id'];
        $res = @$this->_pgdb->query($edit_provinces_sql);
        // 返回结果
        if (!$res) {
            Tools::setHttpHeaders(403);
            return Tools::returnToJson(0x000002, '编辑市失败');
        }
        return Tools::returnToJson(0x000000, '编辑市完成');
    }
}
